package operation

import (
	"../common"
	"../web"
	"encoding/json"
	"strings"
)

type OperDeliveryObj struct {
	common.BaseHandler
}

type OperDeliveryDetailObj struct {
	common.BaseHandler
}

func (self *OperDeliveryObj) ParameterCheck() bool {
	return true
}

func (self *OperDeliveryObj) SqlExecute() bool {
	return true
}

func DeliveryHandler(ctx *web.Context) {
	self := new(OperDeliveryObj)
	self.Ctx = ctx
	self.Html = "deliveryflow.html"
	self.Rightlist = []string{"deliveryflow"}
	self.DoQueryUrl(self)
}

func (self *OperDeliveryDetailObj) ParameterCheck() bool {
	s_pono := strings.TrimSpace(self.Ctx.Params["pono"])
	s_ssns := strings.TrimSpace(self.Ctx.Params["ssn"])

	err := ""
	if s_pono == "" {
		err = common.GetText(self.Lang, "Please input the PO.")
	}
	if s_ssns == "" {
		err = common.GetText(self.Lang, "Please input the Product No.")
	}
	if err != "" {
		dict_js, _ := json.Marshal(map[string]string{"result": err})
		self.Ctx.ResponseWriter.Write(dict_js)
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_pono)
	self.Sqlparas = append(self.Sqlparas, s_ssns)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)

	return true
}

func (self *OperDeliveryDetailObj) SqlQueryExecute() {
	err := ""
	spRtnData := *common.FetchOneRowPtr(
		`sp_ProdDelivery ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		err = common.GetTextSP(self.Lang, spRtnData)
	}

	dict_js, _ := json.Marshal(map[string]string{"result": err})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func DeliveryOperHandler(ctx *web.Context) {
	self := new(OperDeliveryDetailObj)
	self.Ctx = ctx
	self.DoJson(self)
}
